package com.agilegroup.crm.adapters;

import static com.agilegroup.crm.utils.CONSTANTS.SEARCH_BOTH;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.agilegroup.crm.R;
import com.agilegroup.crm.model.BasicView;
import com.agilegroup.crm.model.BasicViewList;

public class AutoCompleteAdapter extends ArrayAdapter<BasicView> implements Filterable {
    
	private List<BasicView> items;
	private Context context;
	
	public AutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        items = new ArrayList<BasicView>();
        this.context=context;
    }

	public int getCount() {
		return items.size();
	}

	public BasicView getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
        if (v == null)
        {
           LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           v = vi.inflate(R.layout.search_row, null);
           
           TextView id = (TextView)v.findViewById(R.id.txtid);
           TextView type = (TextView)v.findViewById(R.id.txtType);
           BasicView basicView = new BasicView();
           basicView = items.get(position);
           id.setText(basicView.getId().toString());
           type.setText(basicView.getName()+" ["+basicView.getType()+"]");
        }
        return v;
	}
	@Override
    public Filter getFilter() {
        Filter myFilter = new Filter() {
			@Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if(constraint != null) {
                	RestTemplate restTemplate = new RestTemplate();
                	List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
                	messageConverters.add(new FormHttpMessageConverter());
                	messageConverters.add(new StringHttpMessageConverter());
                	messageConverters.add(new MappingJacksonHttpMessageConverter());
                	restTemplate.setMessageConverters(messageConverters);
                			
                	String url = SEARCH_BOTH+"1"+"/"+"query="+constraint.toString()+".json";
                	BasicViewList basicViewList = restTemplate.getForObject(url, BasicViewList.class);
                	List<BasicView>basicViews=basicViewList.getBasicViews();
                	items=basicViews;
                    filterResults.values = items;
                    filterResults.count = items.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if(results != null && results.count > 0) {
                notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }
            @Override
            public CharSequence convertResultToString(Object resultValue) {
            	BasicView mobj = BasicView.class.cast(resultValue);
            	return mobj.getName();
            }
        };
        return myFilter;
    }
}